let mask = null; // 底层透明底
let view = null; // 弹窗提示部分
let content_header = null; // 弹窗背景
let log_area = null; // 更新日志
let log = null; // 更新日志
let btn_area = null;
let btn_cancel = null;
let btn_confirm = null;

/*
 *  obj 参数对象
 *   systemInfo  系统参数
 *   log_data   更新日志
 *
 */
 

const viewHotFixInit = (obj) => {   
	let screenWidth = obj.systemInfo.screenWidth;
	let screenHeight = obj.systemInfo.screenHeight;
	let log_data = [];
	let log_area_heigth = obj.log_data.length * 30 + 20;
	let btn_area_top = log_area_heigth + 157;
	let content_height = 157 + log_area_heigth + 40;
	let content_top = (obj.systemInfo.screenHeight - content_height) / 2;
	let content_left = (obj.systemInfo.screenWidth - 250) / 2; 
	 
 
	for (let i = 0; i < obj.log_data.length; i++) {
		let log_item = {
			tag: 'font',
			id: 't' + i,
			text: obj.log_data[i],
			position: {
				top: 30 * i + 'px',
				left: '16px',
				width: '230px',
				height: '50px'
			},
			textStyles: {
				align: 'left',
				color: '#333',
				size: '14px',
				whiteSpace: 'normal'
			}
		}
		log_data.push(log_item)
	}
	
	// 绘制底层遮罩
	if (!mask) {
		mask = new plus.nativeObj.View('viewHotFixMask', {
			bottom: '0px',
			left: '0px',
			right: '0px',
			top: '0px',
		});
		mask.drawRect({
			color: 'rgba(0, 0, 0, 0.5)'
		});
		mask.show();
	}
	
	// 绘制弹窗背景
	if (!content_header) {
		content_header = new plus.nativeObj.View('content_header', {
			top: content_top + 'px',
			left: content_left + 'px',
			height: '157px',
			width: '250px',
			position: 'dock'
		})
		content_header.drawBitmap('/static/images/update_icon.png', {
			top: '0px',
			left: '0px',
			width: '100%',
			height: '100%'
		}, {
			top: 'auto',
			left: 'auto',
			width: '250px',
			height: '157px'
		});
		content_header.drawText('本次更新', {
			top: '0',
			left: '24px',
			width: '100%',
			height: '100%'
		}, {
			align: 'left',
			size: '16px',
			color: '#ffffff'
		}, );
		content_header.drawText('v' + obj.version, {
			top: '20px',
			left: '30px',
			width: '100%',
			height: '100%'
		}, {
			align: 'left',
			size: '16px',
			color: '#ffffff'
		}, );
	
		content_header.show();
	};
	
	// 绘制log区域
	if (!log_area) {
		log_area = new plus.nativeObj.View('log_area', {
			top: content_top + 157 + 'px',
			left: content_left + 'px',
			height: log_area_heigth + 'px',
			width: '250px',
			position: 'dock'
		})
		log_area.drawRect({
			color: 'rgba(255, 255, 255, 1)' // 定义白色颜色
		})
	
		log_area.show();
	}
	
	// 绘制log内容
	if (!log) {
		log = new plus.nativeObj.View('log', {
			top: content_top + 150 + 'px',
			left: content_left + 'px',
			height: log_area_heigth + 'px',
			width: '250px',
			position: 'dock'
		}, log_data) 
	
		log.show();
	}
	
	// 绘制按钮区域
	if (!btn_area) {
		btn_area = new plus.nativeObj.View('btn_area', {
			top: (btn_area_top + content_top) + 'px',
			left: content_left + 'px',
			height: '40px',
			width: '250px',
			position: 'dock'
		})
		btn_area.drawRect({
			color: 'rgba(255, 255, 255, 1)' // 定义白色颜色
		})
	
		btn_area.show();
	}
	 
	// 绘制重启按钮
	if (!btn_confirm) {
		btn_confirm = new plus.nativeObj.View('btn_confirm', {
			top: (btn_area_top + content_top + 5) + 'px',
			left: 188 + 'px',
			height: '25px',
			width: '100px',
			position: 'dock'
		})
		btn_confirm.drawRect({
			color: 'rgba(0,153,255,1)',
			radius: '15px'
		})
		btn_confirm.drawText('立即更新', {
			top: '0',
			left: '0',
			width: '100px',
			height: '25px'
		}, {
			align: 'center',
			size: '14px',
			color: '#ffffff'
		});
		btn_confirm.addEventListener("click", () => {
			if(plus.os.name==="Android"){
				downLoad('https://xmjzapi.isca.cn:1443'+obj.apk_path,obj.version,obj.versionCode)
			}else{ 
				plus.runtime.launchApplication({
				    action: `itms-apps://itunes.apple.com/cn/app/id6443389527?mt=8`
				}, function(e) { 
				});
				//IOS 强制关闭应用
				plus.ios.import('UIApplication').sharedApplication().performSelector('exit');
 
			}
			
		}, false);
	
		btn_confirm.show();
	}
	
	// 绘制取消按钮
	if (!btn_cancel) {
		btn_cancel = new plus.nativeObj.View('btn_cancel', {
			top: (btn_area_top + content_top + 5) + 'px',
			left: 75 + 'px',
			height: '25px',
			width: '100px',
			position: 'dock'
		})
		btn_cancel.drawRect({
			color: 'rgba(204,204,204,1)',
			radius: '15px'
		})
		btn_cancel.drawText('暂不更新', {
			top: '0',
			left: '0',
			width: '100px',
			height: '25px'
		}, {
			align: 'center',
			size: '14px',
			color: '#ffffff'
		});
		btn_cancel.addEventListener("click", () => {
			viewHotFixHide();  // 关闭弹窗
		}, false);
	
		btn_cancel.show();
	}

	

};
 
const downLoad = (url,version,versionCode) => {  
	uni.showLoading({
		title: "新版下载中..."
	}) 
	uni.downloadFile({
		url: url, //仅为示例，并非真实的资源
		success: (res) => {  
			if (res.statusCode === 200) {
				uni.hideLoading();
				plus.nativeUI.showWaiting("正在安装新版本..."); 
				plus.runtime.install(res.tempFilePath, {
					force: true
				}, function(success){ 
					plus.nativeUI.closeWaiting();
					
					setTimeout(() => { 					
						uni.setStorage({
							key: 'now_version',
							data: version
						});
						uni.setStorage({
							key: 'now_versionCode',
							data: versionCode
						});
						plus.runtime.restart(); 
					}, 1000)
				},function(error) {
					uni.showToast({
						icon:'none',
						image:'../static/images/error.png',
						title: '安装失败',
						duration: 1500
					});
				})
			} else { 
				uni.hideLoading();
				uni.showToast({
					icon:'none',
					image:'../static/images/error.png',
					title: '更新失败',
					duration: 1500
				});
			}
		},
		fail: (error) => { 
			uni.hideLoading();
			uni.showToast({
				icon:'none',
				image:'../static/images/error.png',
				title: '更新失败',
				duration: 1500
			});
		} ,
	})
     
}


const viewHotFixHide = () => { 
	if (content_header) { 
		content_header.hide();
		content_header=null;
	}
	if (mask) { 
		mask.hide();
		mask=null;
	}
	if (log_area) { 
		log_area.hide();
		log_area=null;
	}
	if (log) { 
		log.hide();
		log=null;
	}
	if (btn_area) { 
		btn_area.hide();
		btn_area=null;
	} 
	if (btn_confirm) { 
		btn_confirm.hide();
		btn_confirm=null;
	}
	if (btn_cancel) {
		btn_cancel.hide();
		btn_cancel=null;
	}
}
 

export {
	viewHotFixInit,
	viewHotFixHide,
	downLoad 
}
